<template>
  <div class="login-container">
    <div class="login-box">
      <h1 class="title">岳麓书院文化知识管理系统</h1>
      <el-form ref="loginFormRef" :model="loginForm" :rules="rules" class="login-form">
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="loginForm.phone"
            placeholder="请输入手机号"
            class="input"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
            type="password"
            class="input"
          ></el-input>
        </el-form-item>
        <div class="form-options">
          <a class="option" @click="goForgotPwd">忘记密码</a>
          <span class="option" @click="goRegister">注册账号</span>
        </div>
        <el-button type="primary" @click="submitLogin" class="login-button">
          登录
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { ElMessage } from "element-plus";
import { useRouter } from 'vue-router'
import { getUserInfo, userLoginService } from "../api/user";
import { tokenStore } from '../store/token.js'
import { userInfoStore } from '../store/userInfo.js'

const loginForm = ref({
  phone: '',
  password: ''
});

const loginFormRef = ref(null)
const token = tokenStore()
const userInfo = userInfoStore() 

const rules = ref({
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    {
      pattern: /^1[3-9]\d{9}$/, 
      message: '请输入正确的手机号码', 
      trigger: 'blur' 
    }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,10}$/, 
      message: '密码必须是6-10位，包含大小写字母和数字', 
      trigger: 'blur' 
    }
  ]
});

const router = useRouter();

// 登录
const submitLogin = () => {
          router.push('/ordinaryUser/layout')

  return
  loginFormRef.value.validate(async (valid) => {
    if (valid) {
      const res = await userLoginService({
        phone: loginForm.value.phone,
        password: loginForm.value.password
      })

      if(res.data.code === 1) {
        // 将token存入到tokenStore中
        token.setToken(res.data.data)
        const res2 = await getUserInfo({
          token: token
        })
        // 将用户信息存储到userInfo中
        userInfo.setInfo(res2.data.data)    
        ElMessage.success("登录成功")

        if(res2.data.status === 0) {
          // 进入管理员页面

        } else if(res2.data.status === 1) {
          // 进入高级用户页面
          router.push('/advancedUser/layout')
        } else if(res2.data.status === 2) {
          // 进入普通用户页面
          router.push('/ordinaryUser/layout')
        }
        
      }
      
    } else {
      ElMessage.error("验证失败，请检查输入")
    }
  });
};

//注册
const goRegister = () => {
  router.push('/register')
}

//忘记密码
const goForgotPwd = () => {
  router.push('/forgotPassword')
}
</script>

<style scoped>
.login-container {
  background-image:  url('../assets/images/background.png');
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  position: relative;
}

.login-box {
  background-color: rgba(255, 255, 255, 0.8); /* 半透明背景 */
  padding: 30px;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.title {
  font-size: 24px;
  margin-bottom: 20px;
  color: #1f4068;
}

.login-form {
  width: 300px;
}

.input {
  margin-bottom: 15px;
}

.form-options {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
}

.option {
  color: #1f4068;
  text-decoration: none;
}

.login-button {
  width: 100%;
  background-color: #1f4068;
}
</style>
